package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.PostTag;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class PostTagDao extends BaseDao<PostTag> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<PostTag> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<PostTag> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<PostTag> list = run.query(sql, new ResultSetHandler<List<PostTag>>() {
            @Override
            public List<PostTag> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    PostTag postTag = new PostTag();

                    postTag.setId(rs.getInt("id"));
                    postTag.setPostId(rs.getInt("post_id"));
                    postTag.setTagId(rs.getInt("tag_id"));
                    postTag.setWeight(rs.getInt("weight"));

                    list.add(postTag);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  PostTag 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public PostTag selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, post_id, tag_id, weight from mto_post_tag where id = ?";
        PostTag postTag = run.query(sql,new ResultSetHandler<PostTag>() {
            @Override
            public PostTag handle(ResultSet rs) throws SQLException {
                PostTag postTag = null;
                if(rs.next()) {
                    postTag = new PostTag();

                        postTag.setId(rs.getInt("id"));
                        postTag.setPostId(rs.getInt("post_id"));
                        postTag.setTagId(rs.getInt("tag_id"));
                        postTag.setWeight(rs.getInt("weight"));
                }
                return  postTag;
            }
        },params);
        return postTag;
    }

    /**
    *
    * @param postTag 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(PostTag postTag) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_post_tag (  ");
        sql.append("  post_id, tag_id, weight ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            postTag.getPostId(),  
            postTag.getTagId(),  
            postTag.getWeight()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  postTag
    * @return
    * @throws SQLException
    */
    @Override
    public int update(PostTag postTag) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_post_tag           ");
        sql.append(" SET                    ");
            
            sql.append("   post_id  = ?,  ");
            sql.append("   tag_id  = ?,  ");
            sql.append("   weight  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                postTag.getPostId(), 
                postTag.getTagId(), 
                postTag.getWeight(), 
            postTag.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_post_tag where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
